package com.clkj.nangang.mvp.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clkj.nangang.R;
import com.clkj.nangang.util.ActivityManager;
import com.clkj.nangang.util.Res;

/**
 * Created by Administrator on 2017/6/20.
 */

public abstract class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        /*CustomApplication.getInstance().addActivity(this);
        AppManager.getAppManager().addActivity(this);*/
        ActivityManager.addActivity(this);
        super.onCreate(savedInstanceState);

    }

    public abstract void initData();

    public abstract void assignView();

    public abstract void initView();

  /*  LoadingProgressDialog progressDialog;

    public void showProgressDialog(boolean canBack, String loadingMsg) {
        if (progressDialog != null) {
            progressDialog.cancel();
        }

        progressDialog = new LoadingProgressDialog(this, loadingMsg);
        if (canBack) {
            progressDialog.setCancelable(true);
        } else {
            progressDialog.setCancelable(false);
        }
        progressDialog.show();
    }

    public void dismissProgressDialog() {
        if (null != progressDialog && progressDialog.isShowing()) {
            progressDialog.cancel();
        }
    }*/

    private ProgressDialog progDialog;

    public void showLoading() {
        if (progDialog == null) {
            progDialog = new ProgressDialog(this);
            progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progDialog.setIndeterminate(false);
            progDialog.setCancelable(true);
            progDialog.setMessage("正在加载...");
            progDialog.show();
        }
    }

    public void dismissLoading() {
        if (progDialog != null) {
            progDialog.dismiss();
        }
    }

    public RelativeLayout titlebar;
    private ImageView ivBarBack;
    public TextView tvTitle;
    public TextView tvRight;


    public void initTitle(Integer backgroundResId, Integer titleColorResId, String title, boolean isLeftBack, String rightTvStr) {
        titlebar = (RelativeLayout) findViewById(R.id.titlebar);
        ivBarBack = (ImageView) findViewById(R.id.iv_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvRight = (TextView) findViewById(R.id.tv_right);


        if (null != backgroundResId) {
            titlebar.setBackgroundResource(backgroundResId);
        }

        if (null != titleColorResId) {
            tvTitle.setTextColor(Res.getColor(titleColorResId));
        }

        tvTitle.setText(title);

        if (isLeftBack == true) {
            ivBarBack.setVisibility(View.VISIBLE);
            ivBarBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }

        if (null != rightTvStr) {
            tvRight.setText(rightTvStr);
            tvRight.setVisibility(View.VISIBLE);
        }
    }


    public void startAnimActivity(Class<?> cla) {
        this.startActivity(new Intent(this, cla));
    }

    public void startAnimActivity(Intent intent) {
        this.startActivity(intent);
    }

    public void hideSoftInputView() {
        InputMethodManager manager = ((InputMethodManager) this
                .getSystemService(Activity.INPUT_METHOD_SERVICE));
        if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (getCurrentFocus() != null)
                manager.hideSoftInputFromWindow(getCurrentFocus()
                        .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
}
